package top.ddandang.blog.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import top.ddandang.blog.model.pojo.Comment;
import top.ddandang.blog.mapper.CommentMapper;
import top.ddandang.blog.model.query.PageBean;
import top.ddandang.blog.model.vo.CommentRequestVo;
import top.ddandang.blog.model.vo.CommentVo;
import top.ddandang.blog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dozer.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author D
 * @since 2020-06-23
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private Mapper dozerMapper;

    @Override
    public void insertComment(CommentRequestVo commentRequestVo) {
        //实体类进行转换
        Comment comment = dozerMapper.map(commentRequestVo, Comment.class);

        while (true) {
            if(commentMapper.insert(comment) == 1) {
                break;
            }
        }
    }

    @Override
    public Map<String, Object> listCommentVo(PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNo(), pageBean.getPageSize());
        List<CommentVo> commentVoList = commentMapper.listCommentVo(pageBean.getArticleId());

        PageInfo<CommentVo> page = new PageInfo<> (commentVoList);
        long total = page.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("commentVoList", commentVoList);
        map.put("total", total);
        return map;
    }

    @Override
    public int deleteComment(Integer commentId) {
        return commentMapper.deleteById(commentId);
    }

    @Override
    public int updateCommentTop(Integer commentId, Boolean top) {
        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<Comment>()
                .eq("id", commentId)
                .set("topping", top);
        return commentMapper.update(null, updateWrapper);
    }
}
